<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>器械盘点</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
</head>

<body>
<style type="text/css">
	*{
		font-size:12px;
	}
	#QiXieCheckContent{
		padding-bottom: 10px;
	}
	#QiXieCheckInfo{
		width:100%;
		overflow: hidden;
	}
	li{
		float: left;
		list-style: none;
	}
</style>
<script type="text/javascript">
var editIndex = undefined;
//结束正在编辑的行
function endEditing() {
	if (editIndex == undefined) {
		return true
	}
	if ($('#thpqxCheck').datagrid('validateRow', editIndex)) {
		$('#thpqxCheck').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}
//事件：单击单元格的事件
function onClickCell(index, field) {
	if (editIndex != index) {
		if (endEditing()) {
			$('#thpqxCheck').datagrid('selectRow', index).datagrid('beginEdit',
					index);
			var ed = $('#thpqxCheck').datagrid('getEditor', {
				index : index,
				field : field
			});
			if (ed) {
				($(ed.target).data('textbox') ? $(ed.target).textbox(
						'textbox') : $(ed.target)).focus();
			}
			editIndex = index;
		} else {
			setTimeout(function() {
				$('#thpqxCheck').datagrid('selectRow', editIndex);
			}, 0);
		}
	}
}

//结束编辑，同步数据
function onEndEdit(index, row) {
	var ed = $(this).datagrid('getEditor', {
		index : index,
		field : 'dbunit'
	});
	if(ed){
		row.cgUnit = $(ed.target).combobox('getText');
	}else{
		row.cgUnit = null;
	}
}

//删除行
function removeit() {
	var datas=$('#thpqxCheck').datagrid('getSelections');
	for(var i=0;i<datas.length;i++){
        var data=datas[i];
        var index=$('#thpqxCheck').datagrid('getRowIndex',data);
        editIndex=index;
        $('#thpqxCheck').datagrid('deleteRow',index);
    }
}

//应用
function accept() {
	if (endEditing()) {
		//$('#thpqxCheck').datagrid('acceptChanges');
		endEditing();
	}
}

//撤消
function reject() {
	$('#thpqxCheck').datagrid('rejectChanges');
	editIndex = undefined;
}

//获取数据
function getChanges() {
	var rows = $('#thpqxCheck').datagrid('getChanges');
	alert(rows.length + ' rows are changed!');
}

$(function(){
	//根据仓库ID查询所有仓库详情表信息
	$('#pdqxck').combogrid({
		onChange : function(newValue , oldValue){
			$('#pdqxckName').combogrid({
				url: 'selectHisInventoryById', 
				method: 'post', 
				queryParams: {
					ckNo: newValue
				}
			});
			$('#qxCheckInfo').form('clear');
		}
	});
	
	//赋值
	$('#pdqxckName').combogrid({
		onChange : function(newValue , oldValue){
			var kcName=$('#pdqxckName').combogrid('getText');
			$.getJSON("selectHisInventoryByName", {"kcName": kcName}, function(data){
				$('#pdqxcksl').numberbox('setValue',data.kcNumber);
				$('#pdqxsl').numberspinner({max:data.kcNumber});
				$.getJSON("selectHisPurchaseByName", {"cpName": kcName}, function(data){
					$('#pdqxcpph').textbox('setValue',data.cgBatchnumber);
				});
				//盈亏数量
				//alert(kcName+":::"+data.ckNo);
				$.getJSON("selectHisProfitandlossByIDAndName", {"cpName": kcName,"ckNo":data.ckNo}, function(data2){
					//alert(data2);
					//$('#pdqxyksl').numberbox('setValue',data2);
				});
			});
			$('#pdqxcksl').numberbox('clear');
			$('#pdqxyksl').numberbox('clear');
			$('#pdqxcpph').textbox('clear');
		}
	});
	
	//数字微调
	$('#pdqxsl').numberspinner({
		onChange: function (newValue, oldValue) {
			var pdqxcksl=$('#pdqxcksl').numberbox('getValue');
			if(pdqxcksl==''){
				$.messager.alert('温馨提示', '请先选择您需要盘点的产品名称!','info');
				$('#pdqxsl').numberspinner('setValue',0);
				$('#pdqxcksl').numberbox('setValue','');
			}else{
				var jcsl=$('#pdqxcksl').numberbox('getValue');
				var o=parseInt(jcsl)+parseInt(oldValue)-parseInt(newValue);
				$('#pdqxcksl').numberbox('setValue',o);
			}
			$('#pdqxyksl').numberbox('setValue',newValue);
		}
	});
	
	//加入临时表
	$('#add_pdlsTable').linkbutton({
		onClick : function(){
			var pdqxckName=$('#pdqxckName').combogrid('getText');
			var pdqxsl=$('#pdqxsl').numberspinner('getText');
			var pdqxck=$('#pdqxck').combogrid('getText');
			var qxpdAdmin=$('#qxpdAdmin').combogrid('getText');
			var qxpdDate=$('#qxpdDate').datebox('getText');
			var pdqxcksl=$('#pdqxcksl').numberbox('getText');
			var pdqxyksl=$('#pdqxyksl').numberbox('getText');
			var pdqxcpph=$('#pdqxcpph').textbox('getText');
			$.getJSON("selectHisPurchaseByName", {"cpName": pdqxckName}, function(data){
				var unit=data.cgUnit;
				var id=data.cgNo;
				var rows=$('#thpqxCheck').datagrid('getRows');
				var ii=0;
				for(var i=0;i<rows.length;i++){
					var name=rows[i]['hisMakeaninventories.cpName'];
					if(name==pdqxckName){
						ii++;
					}
				}
				if(pdqxckName=='' || qxpdDate=='' || pdqxck=='' || qxpdAdmin=='' || parseInt(pdqxsl)<1 ){
					$.messager.alert("温馨提示", "请把数据填充完整!",'info');
				}else if(ii>0){
					$.messager.alert("提示", "产品已存在临时调拨中，请勿重复添加!");
					$('#qxCheckInfo').form("clear");
				}else{
					$('#thpqxCheck').datagrid('appendRow', {
						'id' : id,
						'hisMakeaninventories.cpName' : pdqxckName,
						'unit' : unit,
						'hisMakeaninventories.pdNumber' : pdqxsl,
						'hisMakeaninventories.ykNumber' : pdqxyksl,
						'hisMakeaninventories.ckNumber' : pdqxcksl,
						'hisMakeaninventories.cpPh' : pdqxcpph
					});
					$('#qxCheckInfo').form("clear");
				}
			});
		}
	});
	
	//盘点
	$('#submit_qxpd').linkbutton({
		onClick : function(){
			//将datagrid中的数据行保存到隐式表单域中
			//1、获取新增的数据行
			var today='${MakeAnInventoryId}'*1;
			var pdqxck=$('#pdqxck').combogrid('getValue');
			var qxpdAdmin=$('#qxpdAdmin').combogrid('getValue');
			var qxpdDate=$('#qxpdDate').datebox('getText');
			var rows = $('#thpqxCheck').datagrid('getChanges');
			var pdState='已结束';
			var ff_QiXieCheck = $("#ff_QiXieCheck");
			//为每一行生成对应的隐藏表单域
			for(var i = 0; i < rows.length; i ++){
				$("<input type='hidden' x='y'>").attr("name",
						"hisMakeaninventories[" + i + '].pdNo').val(
								today+i).appendTo(ff_QiXieCheck);
				$("<input type='hidden' x='y'>").attr("name",
						"hisMakeaninventories[" + i + '].pdState').val(
								pdState).appendTo(ff_QiXieCheck);
				$("<input type='hidden' x='y'>").attr("name",
						"hisMakeaninventories[" + i + '].pdDate').val(
								qxpdDate).appendTo(ff_QiXieCheck);
				$("<input type='hidden' x='y'>").attr("name",
						"hisMakeaninventories[" + i + '].ckNo').val(
								pdqxck).appendTo(ff_QiXieCheck);
				$("<input type='hidden' x='y'>").attr("name",
						"hisMakeaninventories[" + i + '].ygNo').val(
								qxpdAdmin).appendTo(ff_QiXieCheck);
				generateHidden(rows[i], "cpName", i);
				generateHidden(rows[i], "pdNumber", i);
				generateHidden(rows[i], "ykNumber", i);
				generateHidden(rows[i], "ckNumber", i);
				generateHidden(rows[i], "cpPh", i);
			}
			//提交表单
			if(rows.length != 0){
				$('#ff_QiXieCheck').form('submit', {
					success: function(v){
						var json = eval("(" + v + ")");
						//alert(json);
						if(json=='操作成功'){
							$(":hidden[x='y']").remove();
							$('#thpqxCheck').datagrid('loadData', { total: 0, rows: [] });
							$.messager.alert('温馨提示', "盘点"+json);
							$('#ff_QiXieCheck').form("clear");
							$("#win-QiXieCheck").window("close");
						}else{
							$.messager.alert('温馨提示', json,'info');
						}
					}
				});
			}else{
				$.messager.alert('温馨提示', "请把数据填充完整!",'info');
			}
		}
	});
	
	function generateHidden(row, name, i) {
		var ff_QiXieCheck = $("#ff_QiXieCheck");
		$("<input type='hidden' x='y'>").attr("name",
				"hisMakeaninventories[" + i + '].'  + name).val(
				row['hisMakeaninventories.'+name]).appendTo(ff_QiXieCheck);
	}
})
</script>

	<!-- 器械盘点 -->
	<!-- <div id="win-QiXieCheck" class="easyui-window" title="器械盘点" data-options="collapsible:false,
		minimizable:false,
		maximizable:false,
		modal:true,
		iconCls:'icon-thpCheck'"
		style="width: 806px; height: 500px;"> -->
		<form id="ff_QiXieCheck" action="addQiXieMakeAnInventory" method="post">
			<div id="QiXieCheckContent">
				<div id="QiXieCheckInfo" style="background-color: rgb(236,243,255)">
					<ul>
						<li style="margin:0px 130px 5px 0px;">盘点单号&nbsp;<input value="${MakeAnInventoryId }" class="easyui-textbox" data-options="width: 150" readonly="readonly"></li>
						<li style="margin:0px 90px 5px 0px;">状态&nbsp;<select class="easyui-combobox" data-options="width: 80" readonly="readonly">
							<option value="未结束" selected="selected">未结束</option><option value="已结束">已结束</option></select></li>
						<li style="margin:0px 0px 5px 0px;">还回日期&nbsp;<select id="qxpdDate" class="easyui-datebox" data-options="width: 150"></select></li>
						<li style="margin:0px 118px 10px 0px;">盘点仓库&nbsp;<input id="pdqxck" class="easyui-combogrid" data-options="width: 150,
							url:'selectAllHisWarehouse',
							idField:'ckNo',
							panelHeight:'auto',
							textField:'ckName',
							columns:[[
					            {field:'ckName',title:'仓库名称',width:'146px'}
							]]"></li>
						<li style="margin:0px 0px 10px 0px;">仓管员&nbsp;<input id="qxpdAdmin" class="easyui-combogrid" data-options="width: 150,
							url:'findAllEmployeeWarehouse',
							idField:'ygNo',
							panelHeight:'auto',
							panelWidth:225,
							textField:'ygName',
							columns:[[
					            {field:'ygName',title:'姓名',width:50},
					            {field:'bmName',title:'部门',width:75},
					            {field:'ygPost',title:'职务',width:92}
							]]"></li>
					</ul>
				</div>
				<div style="border-top:1px solid #95B8e7;width:100%;height:346px;overflow: hidden;">
			        <div style="border-bottom:1px solid #95B8e7;">
			        	<div id="qxCheckInfo" style="overflow: hidden;" >
			        		<ul>
								<li style="margin:5px 0px 0px 0px;">器械&nbsp;<select id="pdqxckName" class="easyui-combogrid" data-options="width: 174,
									idField:'kcNo',
									panelHeight:'auto',
									textField:'kcName',
									columns:[[{field:'kcName',title:'仓库器械名称',width:'170px'}
									]]"></select></li>
								<li style="margin:5px 0px 0px 25px;">结存数量&nbsp;<input id="pdqxcksl" class="easyui-numberbox" data-options="width: 100,precision:2" readonly="readonly"></li>
								<li style="margin:5px 0px 0px 25px;">盘点数量&nbsp;<input id="pdqxsl" class="easyui-numberspinner" value="0" data-options="width: 100,min:0,editable:true,precision:0"></li>
								<li style="margin:5px 0px 0px 23px;" title="-(负)报损,+(正)报益">盈亏数量&nbsp;<input id="pdqxyksl" class="easyui-numberbox" value="0" data-options="min:0,width: 100,precision:2" readonly="readonly"></li>
								<li style="margin:10px 0px 10px 0px;">批号&nbsp;<input id="pdqxcpph" class="easyui-textbox" data-options="width: 174" readonly="readonly"></li>
								<li style="margin:10px 0px 10px 446px;"><a id="add_pdlsTable" class="easyui-linkbutton" style="width:80px;">加入</a></li>
							</ul>
			        	</div>
			        	<div style="border:0px red solid;height:252px;">
							<table id="thpqxCheck" class="easyui-datagrid"
								data-options="
									fitColumns:true,
									fit:true,
									singleSelect: true,
									rownumbers: true,
									toolbar: '#qxChecktb',
									border:false
									">
								<thead>
									<tr>
										<th data-options="field:'id',width:'15%'">器械编号</th>
										<th data-options="field:'hisMakeaninventories.cpName',width:'15%'">器械名称</th>
										<th data-options="field:'unit',width:'10%'">单位</th>
										<th data-options="field:'hisMakeaninventories.pdNumber',width:'15%'">盘点数量</th>
										<th data-options="field:'hisMakeaninventories.ykNumber',width:'15%'">盈亏数量</th>
										<th data-options="field:'hisMakeaninventories.ckNumber',width:'15%'">结存数量</th>
										<th data-options="field:'hisMakeaninventories.cpPh',width:'15%'">批号</th>
									</tr>
								</thead>
							</table>
							<div id="qxChecktb" style="height: auto;padding-left: 30px;">
								<a href="javascript:void(0)" class="easyui-linkbutton"
								data-options="iconCls:'icon-add',plain:true">添加一行</a>
								<a href="javascript:void(0)" class="easyui-linkbutton"
									data-options="iconCls:'icon-remove',plain:true"
									onclick="removeit()">移除一行</a> <a href="javascript:void(0)"
									class="easyui-linkbutton"
									data-options="iconCls:'icon-ok',plain:true">应用</a>
								<a href="javascript:void(0)" class="easyui-linkbutton"
									data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a>
							</div>
			        	</div>
			        </div>
			    </div>
			    <div style="margin:5px 10px 0px 0px;text-align: right;">
					<a id="submit_qxpd" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">盘点</a>
					<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:$('#win-QiXieCheck').window('close');" onclick="">取消</a>
				</div>
			</div>
		</form>
	<!-- </div> -->
</body>
</html>